import {createMemoryHistory, createRouter} from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/auth/Login.vue'
import {useAuthStore} from "../utils/store.ts";

export const router = createRouter({
    history: createMemoryHistory(),
    routes: [
        {path:'/',name:'home', component: Home},
        {path:'/login',name:'login', component: Login}

    ]
})

router.beforeEach((to, _from, next) => {
    let authStore = useAuthStore()
    let token = authStore.token
    if(to.name!=='login' && !token){
        next({name: 'login',query:{redirect: to.fullPath}})
    }else {
        next()
    }
})